def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

x = int(input())
if x >= 1000 and x <= 9999:
    a = x % 10
    f = x // 10
    g = f % 10
    s = x // 1000
    d = x // 100
    l = d % 10

    r = 0

    test = s
    if is_prime(test):
        if test > r:
            r = test

    test = l
    if is_prime(test):
        if test > r:
            r = test

    test = g
    if is_prime(test):
        if test > r:
            r = test

    test = a
    if is_prime(test):
        if test > r:
            r = test

    test = 10 * s + l
    if is_prime(test):
        if test > r:
            r = test

    test = 10 * l + g
    if is_prime(test):
        if test > r:
            r = test

    test = 10 * g + a
    if is_prime(test):
        if test > r:
            r = test

    test = 100 * s + 10 * l + g
    if is_prime(test):
        if test > r:
            r = test


    test = 100 * l + 10 * g + a
    if is_prime(test):
        if test > r:
            r = test


    test = x
    if is_prime(test):
        if test > r:
            r = test


    print(r)


else:
    print(0)